<?php 
require_once 'init.php';
require_once 'ckLogin.php';
require_once 'dbClasses'.DIRECTORY_SEPARATOR.'config.php';
require_once 'dbClasses'.DIRECTORY_SEPARATOR.'dbConn.php';
$db = new dbConn();

//分页
$page = isset($_GET['page']) ? $_GET['page'] : 1;
$pagesize = isset($_GET['pagesize']) ? $_GET['pagesize'] : 30;
$sort = isset($_GET['sort']) ? $_GET['sort'] : "CreateTime_desc";
$startTime = isset($_GET['startTime']) ? $_GET['startTime'] : "";
$endTime = isset($_GET['endTime']) ? $_GET['endTime'] : "";
$updater = isset($_GET['updater']) ? $_GET['updater'] : "";

$SortCondition = explode("_", $sort);
$SortStr = " $SortCondition[0] $SortCondition[1]";

$where = "";
$url = "?sort=$sort&pagesize=$pagesize";
if($startTime || $endTime || $updater)
{
	$where .= ($startTime) ? " AND o.CreateTime>='{$startTime}' " : "";
	 $url  .= ($startTime) ? "&startTime=$startTime" : "";
	$where .= ($endTime) ? " AND o.CreateTime<='{$endTime}' " : "";
	 $url  .= ($endTime) ? "&endTime=$endTime" : "";
	$where .= ($updater) ? " AND c.updater='{$updater}' " : "";
	 $url  .= ($updater) ? "&updater=$updater" : "";
}

//总条数
$db->sql="SELECT count(o.GoodsId) FROM OrderData o,((SELECT con.`goodsID`,con.`updater`,con.`publishDate` from content con where con.`status`=1) union(SELECT p.`goodsID`,p.`updater`,p.`publishDate` from priceninepointnine p where p.`status`=1)) c, admin a where o.GoodsId=c.goodsID AND c.updater=a.userName AND month(c.`publishDate`)=month(o.`CreateTime`) $where";
$count = $db->doResultOne();
$db->sql="SELECT count(GoodsId) FROM OrderData";
$count2 = $db->doResultOne();

$pageInfo = page($count, $pagesize, $page);
$maxPage = $pageInfo['maxPage'];
$start = $pageInfo['start'];
$pages = $pageInfo['pages'];

$db->sql="SELECT o.*,a.nickName FROM OrderData o,((SELECT con.`goodsID`,con.`updater`,con.`publishDate` from content con where con.`status`=1) union(SELECT p.`goodsID`,p.`updater`,p.`publishDate` from priceninepointnine p where p.`status`=1)) c,admin a where o.GoodsId=c.goodsID AND c.updater=a.userName AND month(c.`publishDate`)=month(o.`CreateTime`) $where ORDER BY $SortStr LIMIT $start,$pagesize ";
//var_dump($db->sql);
$datas = $db->doResultList2();


$admins = getAdmin($db);
//var_dump($admins);
//用户列表
function getAdmin($db){
	$db->sql = "SELECT * FROM admin WHERE type=2";
	return $admins = $db->doResultList2();
}
//分页
function page($total,$size,$page){
	//最大页数
	if($total%$size==0){
		$maxPage = $total/$size;
	}else{
		$maxPage = (int)($total/$size)+1;
	}
	//开始条数
	$start = ($page-1)*$size;
	//分页
	if($maxPage>=5){
		if($page<=2){
			$pages=array(1,2,3,4,5);
		}elseif($page>=($maxPage-1)){
			$pages=array($maxPage-4,$maxPage-3,$maxPage-2,$maxPage-1,$maxPage);
		}else{
			$pages=array($page-2,$page-1,$page,$page+1,$page+2);
		}
	}else{
		for($i=1;$i<=$maxPage;$i++){
			$pages[]=$i;
		}
	}
	if($page<=1){$previous=1;}else{$previous=$page-1;} 
	if($page>=$maxPage) {$next = $maxPage;}else{ $next = $page+1;}
	return array('maxPage'=>$maxPage,'start'=>$start,'pages'=>$pages,'previous'=>$previous,'next'=>$next);
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Simpla Admin</title>
<!--                       CSS                       -->
<!-- Reset Stylesheet -->
<link rel="stylesheet" href="resources/css/reset.css" type="text/css"
	media="screen" />
<!-- Main Stylesheet -->
<link rel="stylesheet" href="resources/css/main.css" type="text/css"
	media="screen" />
<!-- Invalid Stylesheet. This makes stuff look pretty. Remove it if you want the CSS completely valid -->
<link rel="stylesheet" href="resources/css/invalid.css" type="text/css"
	media="screen" />
<!-- Colour Schemes
		Default colour scheme is green. Uncomment prefered stylesheet to use it.
		<link rel="stylesheet" href="resources/css/blue.css" type="text/css" media="screen" />
		<link rel="stylesheet" href="resources/css/red.css" type="text/css" media="screen" />  
		-->
<!-- Internet Explorer Fixes Stylesheet -->
<!--[if lte IE 7]>
			<link rel="stylesheet" href="resources/css/ie.css" type="text/css" media="screen" />
		<![endif]-->
<!--                       Javascripts                       -->
<!-- jQuery -->
<script type="text/javascript" src="resources/scripts/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
$(function($) {
	//导出
	$("#download").click(function(){
		var startTime = $('#startTime').val();
		var endTime   = $('#endTime').val();
		if(startTime && endTime){
			if(startTime > endTime){
				alert('开始时间大于结束时间');
				return false;
			}else{
				window.open('downloadExcel4.php?startTime='+startTime+'&endTime='+endTime);
			}
		}else{
			alert("时间不能为空");
			return false;
		}
	});
})
</script>
<!-- jQuery Configuration -->
<script type="text/javascript" src="resources/scripts/simpla.jquery.configuration.js"></script>
<!-- Facebox jQuery Plugin -->
<script type="text/javascript" src="resources/scripts/facebox.js"></script>
<!-- jQuery WYSIWYG Plugin -->
<script type="text/javascript" src="resources/scripts/jquery.wysiwyg.js"></script>
<!-- jQuery my97datepicker -->
<script type="text/javascript" src="resources/scripts/My97DatePicker/WdatePicker.js"></script>
<!-- jQuery colorbox -->
<link rel="stylesheet" href="resources/css/colorbox.css" />
<script src="resources/scripts/colorbox/jquery.colorbox.js"></script>
</head>
<body>
	<div id="body-wrapper">
		<!-- Wrapper for the radial gradient background -->		
		<div id="main-content">
			<!-- Main Content Section with everything -->
			<!-- Page Head -->
			<h2>欢迎使用<?php echo APPLICATION;?>管理后台</h2>
			<p id="page-intro">接下来你要做什么呢亲？</p>
			<div class="clear"></div>
			<!-- End .clear -->
			<div class="content-box">
				<!-- Start Content Box -->
				<div class="content-box-header">
					<h3>订单数据列表</h3>
					<!-- <ul class="content-box-tabs">
						<li><a href="#tab1" class="default-tab">Table</a></li>
						href must be unique and match the id of target div
						<li><a href="#tab2">Forms</a></li>
					</ul> -->
					<div class="clear"></div>
				</div>
				<!-- End .content-box-header -->
				<div class="content-box-content">
					<form method="get" action="">
						时间：
						<input type="text" id="startTime" name="startTime" onClick="WdatePicker()" value="<?php echo $startTime;?>" size="10"/> 
						-
						<input type="text" id="endTime" name="endTime" onClick="WdatePicker()" value="<?php echo $endTime;?>" size="10"/>
						&nbsp;&nbsp;&nbsp;供稿人：
						<select name="updater" style="">
							<option value="" ></option>
							<?php foreach ($admins as $keyX=>$itemX):?>
							<option id="" value="<?php echo $itemX['userName'];?>" ><?php echo $itemX['nickName'];?></option>
							<?php endforeach;?>
						</select>
						&nbsp;&nbsp;&nbsp;
						每页<input name="pagesize" type="text" size="2" value="<?php echo $pagesize;?>"/>条
						&nbsp;&nbsp;&nbsp;
						<button class="button" id="zhixing" type="submit">搜索</button>
						<button class="button" id="download" type="submit">导出</button>												
					</form>
					
					<hr />
					<div class="clear"></div>
					<div class="tab-content default-tab" id="tab1">
						<!-- This is the target div. id must match the href of this div's tab -->

						<table>
							<thead>
								<tr>
									<td colspan="11">										 
										<?php if($maxPage>0):?>
										<div class="pagination">
											<a href="<?php echo $url;?>&page=1" title="First Page">&laquo; First</a>
											<a href="<?php echo $url;?>&page=<?php echo $pageInfo['previous'];?>" title="Previous Page">&laquo; Previous</a>
											<?php foreach ($pages as $item):?> 
											<a href="<?php echo $url;?>&page=<?php echo $item;?>" class="number <?php if($item == $page) echo 'current';?>" title="<?php echo $item;?>"><?php echo $item;?></a>
											<?php endforeach;?>
											<a href="<?php echo $url;?>&page=<?php echo $pageInfo['next'];?>" title="Next Page">Next &raquo;</a>
											<a href="<?php echo $url;?>&page=<?php echo $maxPage;?>" title="Last Page">Last &raquo;</a>
											<div style="margin-top: -40px; position: absolute; right: 54px;">
												total: <?php echo $maxPage;?>页　data: <?php echo $count;?>条/<?php echo $count2;?>条
											</div>
										</div> 
										<?php endif;?>
										<!-- End .pagination -->
										<hr class="clear" />										
									</td>
								</tr>
							</thead>
							<thead>
								<tr>
									<th>行号</th>									
									<th>
										商品编号
										<br>
										<span>
											<a href="?page=1&sort=GoodsId_asc">
											<?php if($sort == "GoodsId_asc"):?>
												<img src="resources/images/sort3.png">
											<?php else:?>
												<img src="resources/images/sort1.png">
											<?php endif;?>
											</a>
											<a href="?page=1&sort=GoodsId_desc">
											<?php if($sort == "GoodsId_desc"):?>
												<img src="resources/images/sort4.png">
											<?php else:?>
												<img src="resources/images/sort2.png">
											<?php endif;?>
											</a>
										</span>
									</th>									
									<th>
										创建日期
										<br>
										<span>
											<a href="?page=1&sort=CreateTime_asc">
											<?php if($sort == "CreateTime_asc"):?>
												<img src="resources/images/sort3.png">
											<?php else:?>
												<img src="resources/images/sort1.png">
											<?php endif;?>
											</a>
											<a href="?page=1&sort=CreateTime_desc">
											<?php if($sort == "CreateTime_desc"):?>
												<img src="resources/images/sort4.png">
											<?php else:?>
												<img src="resources/images/sort2.png">
											<?php endif;?>
											</a>
										</span>
									</th>
									<th>
										订单数量
										<br>
										<span>
											<a href="?page=1&sort=OrderNum_asc">
											<?php if($sort == "OrderNum_asc"):?>
												<img src="resources/images/sort3.png">
											<?php else:?>
												<img src="resources/images/sort1.png">
											<?php endif;?>
											</a>
											<a href="?page=1&sort=OrderNum_desc">
											<?php if($sort == "OrderNum_desc"):?>
												<img src="resources/images/sort4.png">
											<?php else:?>
												<img src="resources/images/sort2.png">
											<?php endif;?>
											</a>
										</span>
									</th>
									<th>
										商品原价
										<br>
										<span>
											<a href="?page=1&sort=GoodsPrice_asc">
											<?php if($sort == "GoodsPrice_asc"):?>
												<img src="resources/images/sort3.png">
											<?php else:?>
												<img src="resources/images/sort1.png">
											<?php endif;?>
											</a>
											<a href="?page=1&sort=GoodsPrice_desc">
											<?php if($sort == "GoodsPrice_desc"):?>
												<img src="resources/images/sort4.png">
											<?php else:?>
												<img src="resources/images/sort2.png">
											<?php endif;?>
											</a>
										</span>
									</th>
									<th>
										商品现价
										<br>
										<span>
											<a href="?page=1&sort=CurrentPrice_asc">
											<?php if($sort == "CurrentPrice_asc"):?>
												<img src="resources/images/sort3.png">
											<?php else:?>
												<img src="resources/images/sort1.png">
											<?php endif;?>
											</a>
											<a href="?page=1&sort=CurrentPrice_desc">
											<?php if($sort == "CurrentPrice_desc"):?>
												<img src="resources/images/sort4.png">
											<?php else:?>
												<img src="resources/images/sort2.png">
											<?php endif;?>
											</a>
										</span>
									</th>
									<th>
										佣金比例
										<br>
										<span>
											<a href="?page=1&sort=OrderRate_asc">
											<?php if($sort == "OrderRate_asc"):?>
												<img src="resources/images/sort3.png">
											<?php else:?>
												<img src="resources/images/sort1.png">
											<?php endif;?>
											</a>
											<a href="?page=1&sort=OrderRate_desc">
											<?php if($sort == "OrderRate_desc"):?>
												<img src="resources/images/sort4.png">
											<?php else:?>
												<img src="resources/images/sort2.png">
											<?php endif;?>
											</a>
										</span>
									</th>
									<th>佣金</th>
									<th>订单状态</th>
									<th>
										创建者
										<br>
										<span>
											<a href="?page=1&sort=nickName_asc">
											<?php if($sort == "nickName_asc"):?>
												<img src="resources/images/sort3.png">
											<?php else:?>
												<img src="resources/images/sort1.png">
											<?php endif;?>
											</a>
											<a href="?page=1&sort=nickName_desc">
											<?php if($sort == "nickName_desc"):?>
												<img src="resources/images/sort4.png">
											<?php else:?>
												<img src="resources/images/sort2.png">
											<?php endif;?>
											</a>
										</span>
									</th>
									<th>
										更新日期
										<br>
										<span>
											<a href="?page=1&sort=UpdateTime_asc">
											<?php if($sort == "UpdateTime_asc"):?>
												<img src="resources/images/sort3.png">
											<?php else:?>
												<img src="resources/images/sort1.png">
											<?php endif;?>
											</a>
											<a href="?page=1&sort=UpdateTime_desc">
											<?php if($sort == "UpdateTime_desc"):?>
												<img src="resources/images/sort4.png">
											<?php else:?>
												<img src="resources/images/sort2.png">
											<?php endif;?>
											</a>
										</span>
									</th>									
								</tr>

							</thead>

							<tbody>
							<?php if(count($datas)>0):?>
							<?php foreach ($datas as $key => $item):?>
								<tr>
									<td><?php echo ($key+1);?></td>									
									<td><?php echo $item['GoodsId'];?></td>
									<td><?php echo $item['CreateTime'];?></td>
									<td><?php echo $item['OrderNum'];?></td>
									<td><?php echo $item['GoodsPrice'];?></td>
									<td><?php echo $item['CurrentPrice'];?></td>
									<td><?php echo $item['OrderRate'];?></td>
									<td><?php if($item['OrderRate']) echo $item['GoodsPrice']*trim($item['OrderRate'], "%")*0.01;?></td>
									<td><?php echo $item['OrderState'];?></td>
									<td><?php echo $item['nickName'];?></td>
									<td><?php echo $item['UpdateTime'];?></td>
								</tr>
							<?php endforeach;?>
							<?php else:?>
								<tr style="font-size: 50px;text-align: center; font-weight: bold;"><td colspan="9" style="text-align: center;">没有订单数据</td></tr>
							<?php endif;?>
							</tbody>
							<tfoot>
								<tr>
									<td colspan="11">										 
										<?php if($maxPage>0):?>
										<div class="pagination">
											<a href="<?php echo $url;?>&page=1" title="First Page">&laquo; First</a>
											<a href="<?php echo $url;?>&page=<?php echo $pageInfo['previous'];?>" title="Previous Page">&laquo; Previous</a>
											<?php foreach ($pages as $item):?> 
											<a href="<?php echo $url;?>&page=<?php echo $item;?>" class="number <?php if($item == $page) echo 'current';?>" title="<?php echo $item;?>"><?php echo $item;?></a>
											<?php endforeach;?>
											<a href="<?php echo $url;?>&page=<?php echo $pageInfo['next'];?>" title="Next Page">Next &raquo;</a>
											<a href="<?php echo $url;?>&page=<?php echo $maxPage;?>" title="Last Page">Last &raquo;</a>
											<div style="margin-top: -40px; position: absolute; right: 54px;">
												total: <?php echo $maxPage;?>页　data: <?php echo $count;?>条/<?php echo $count2;?>条
											</div>
										</div> 
										<?php endif;?>
										<!-- End .pagination -->
										<hr class="clear" />										
									</td>
								</tr>
							</tfoot>

						</table>

					</div>
					<!-- End #tab1 -->


				</div>
				<!-- End .content-box-content -->

			</div>
			<!-- End .content-box -->

			<div class="clear"></div>

			<div id="footer">
				<small> <!-- Remove this notice or replace it with whatever you want -->
					&#169; Copyright 2009 Your Company | Powered by <a
					href="http://themeforest.net/item/simpla-admin-flexible-user-friendly-admin-skin/46073">Simpla
						Admin</a> | <a href="#">Top</a>
				</small>
			</div>
			<!-- End #footer -->

		</div>
		<!-- End #main-content -->

	</div>
</body>


<!-- Download From www.exet.tk-->
</html>
